﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class PringDocumentSetApp : IPringDocumentSet
    {
        private string dbname = "Systems";

        public void Add(PringDocumentSetEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_PringDocumentSet(");
            builder.Append("ChainNum,DocumentType,ChainFax,ChainWebAddress,OtherPrintString");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("" + model.DocumentType + ",");
            builder.Append("'" + model.ChainFax + "',");
            builder.Append("'" + model.ChainWebAddress + "',");
            builder.Append("'" + model.OtherPrintString + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int DocumentId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_PringDocumentSet ");
            builder.Append(" where DocumentId=" + DocumentId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PringDocumentSet ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by DocumentId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "DocumentId", "T_PringDocumentSet", swhere);
        }

        public PringDocumentSetEnt GetModel(int DocumentId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DocumentId,ChainNum,DocumentType,ChainFax,ChainWebAddress,OtherPrintString from T_PringDocumentSet ");
            builder.Append(" where DocumentId=" + DocumentId);
            PringDocumentSetEnt ent = new PringDocumentSetEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.DocumentId = DocumentId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                if (table.Rows[0]["DocumentType"].ToString() != "")
                {
                    ent.DocumentType = int.Parse(table.Rows[0]["DocumentType"].ToString());
                }
                ent.ChainFax = table.Rows[0]["ChainFax"].ToString();
                ent.ChainWebAddress = table.Rows[0]["ChainWebAddress"].ToString();
                ent.OtherPrintString = table.Rows[0]["OtherPrintString"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PringDocumentSet ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "DocumentId");
        }

        public void Update(PringDocumentSetEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_PringDocumentSet set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DocumentType=" + model.DocumentType + ",");
            builder.Append("ChainFax='" + model.ChainFax + "',");
            builder.Append("ChainWebAddress='" + model.ChainWebAddress + "',");
            builder.Append("OtherPrintString='" + model.OtherPrintString + "'");
            builder.Append(" where DocumentId=" + model.DocumentId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

